//
//  HDHNavigationController.m
//  无忧社区
//
//  Created by hdh1997 on 15/11/7.
//  Copyright © 2015年 hdh1997. All rights reserved.
//

#import "HDHNavigationController.h"
#import "UIImage+HDHImage.h"



@interface HDHNavigationController ()<UINavigationControllerDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, strong) id popDelegate;

@end

@implementation HDHNavigationController

// 第一次使用当前类或者它的子类的时候调用
// 作用:初始化类

+(void)initialize
{
    //当前类初始化的时候才会调用
    if (self == [HDHNavigationController class]) {
        
        //获取当前类下面的导航条
        UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
        //设置导航条的背景图片
        // UIBarMetricsDefault只有设置这个,才能够设置导航条背景图片
        // UIBarMetricsDefault导致导航控制器的子控制器的view尺寸改变
        [navBar setBackgroundImage:[UIImage imageNamed:@"n_color_2"] forBarMetrics:UIBarMetricsDefault];
        
        //设置主题颜色
        //        navBar.tintColor = [UIColor whiteColor];
        
        NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
        dictM[NSForegroundColorAttributeName] = [UIColor whiteColor];
        
        //修改导航条字体的颜色
        [navBar setTitleTextAttributes:dictM];
        
        //1.导航条上的内容是由UIBarButtonItem决定的
        //2.获取当前类里面的所有UIBarButtonItem
        //
        //        UIBarButtonItem *item = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
        //
        //        [item setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, 0) forBarMetrics:UIBarMetricsDefault];
        
        
        
    }
    
}


-(void)viewDidLoad
{
    [super viewDidLoad];
    
    //恢复滑动返回功能
    _popDelegate = self.interactivePopGestureRecognizer.delegate;
    
    //设置代理,目的监听控制器有没有完全显示
    self.delegate = self;
    
    //为什么会有滑动返回功能,苹果实现,每次触发滑动手势,都会调用苹果某个对象方法
    id ges = self.interactivePopGestureRecognizer;
    
    //禁止使用系统自带的手势
    self.interactivePopGestureRecognizer.enabled = NO;
    
    //自己创建一个全屏手势,调用系统的滑动返回功能
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
}



#pragma mark -UIGestureRecognizerDelegate

//判断下是否允许触发当前手势
-(BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    //如果是根控制器就触发手势
    return self.childViewControllers.count != 1;
    
}


#pragma mark -UINavigationControllerDelegate

//每次跳转控制器的时候都会
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    //判断下是否是根控制器
    if (self.childViewControllers.count != 0) {
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageWithOriginalImageName:@"NavBack"] style:UIBarButtonItemStyleBordered target:self action:@selector(back)];
    }
    
    //设置导航控制器的左边按钮
    
    
    [super pushViewController:viewController animated:animated];
    
    
}


//返回上一个界面
-(void)back
{
    
    [self popViewControllerAnimated:YES];
}



@end
